package cn.iocoder.yudao.module.cmt.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentPostDO;
import cn.iocoder.yudao.module.cmt.service.content.ContentPostService;

@Tag(name = "管理后台 - 帖子")
@RestController
@RequestMapping("/cmt/content-post")
@Validated
public class ContentPostController {

    @Resource
    private ContentPostService postService;

    @PostMapping("/create")
    @Operation(summary = "创建帖子")
    @PreAuthorize("@ss.hasPermission('cmt:content-post:create')")
    public CommonResult<Long> createPost(@Valid @RequestBody ContentPostSaveReqVO createReqVO) {
        return success(postService.createPost(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新帖子")
    @PreAuthorize("@ss.hasPermission('cmt:content-post:update')")
    public CommonResult<Boolean> updatePost(@Valid @RequestBody ContentPostSaveReqVO updateReqVO) {
        postService.updatePost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除帖子")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:content-post:delete')")
    public CommonResult<Boolean> deletePost(@RequestParam("id") Long id) {
        postService.deletePost(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除帖子")
    @PreAuthorize("@ss.hasPermission('cmt:content-post:delete')")
    public CommonResult<Boolean> deletePostList(@RequestParam("ids") List<Long> ids) {
        postService.deletePostListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得帖子")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:content-post:query')")
    public CommonResult<ContentPostRespVO> getPost(@RequestParam("id") Long id) {
        ContentPostDO post = postService.getPost(id);
        return success(BeanUtils.toBean(post, ContentPostRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子分页")
    @PreAuthorize("@ss.hasPermission('cmt:content-post:query')")
    public CommonResult<PageResult<ContentPostRespVO>> getPostPage(@Valid ContentPostPageReqVO pageReqVO) {
        PageResult<ContentPostDO> pageResult = postService.getPostPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContentPostRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出帖子 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:content-post:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPostExcel(@Valid ContentPostPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentPostDO> list = postService.getPostPage(pageReqVO).getList();
        ExcelUtils.write(response, "帖子.xls", "数据", ContentPostRespVO.class,
                        BeanUtils.toBean(list, ContentPostRespVO.class));
    }

}
